package com.losdun.aftersales.controller;

import com.losdun.aftersales.controller.req.WorkOrderFinishedReq;
import com.losdun.aftersales.controller.req.WorkerOrderQuery;
import com.losdun.aftersales.domain.Install;
import com.losdun.aftersales.domain.Repair;
import com.losdun.aftersales.service.IInstallService;
import com.losdun.aftersales.service.IRepairService;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.common.sms.AliyunSmsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 小程序师傅控制器
 * @Author Sword.Ye
 * @Date 2025/10/16
 **/
@RestController
@RequestMapping("/worker")
@Slf4j
public class WorkerController extends BaseController {

    @Autowired
    private IInstallService installService;
    @Autowired
    private IRepairService repairService;
    @Autowired
    private AliyunSmsClient aliyunSmsClient;

    @GetMapping("/order/install/list")
    public TableDataInfo installList(WorkerOrderQuery query)
    {
        startPage();
        Install install = new Install();
        install.setWorkerId(query.getWorkerId());
        install.setType(query.getType());
        List<Install> list = installService.selectInstallList(install);
        return getDataTable(list);
    }

    @GetMapping("/order/install/getFinishCode")
    public AjaxResult getInstallFinishCode(Long id)
    {
        Install install = installService.selectInstallById(id);
        if(install!=null && install.getFinishCode()!=null){
            //发送验证码
            Map<String,String> paramsMap = new HashMap<>();
            paramsMap.put("code",install.getFinishCode());
            paramsMap.put("orderSn",install.getWorkNo());
            aliyunSmsClient.sendSms("work",install.getContactNumber(),paramsMap);
            return AjaxResult.success();
        }
        return AjaxResult.error("验证码发送失败！");
    }
    @PostMapping("/order/install/completed")
    public AjaxResult completedInstall(@RequestBody WorkOrderFinishedReq req) {
        try {
            installService.completedInstall(req.getId(), req.getFinishCode());
        } catch (Exception e) {
           log.error("工单完成失败,errMsg:{}",e.getMessage(),e);
           return error("工单完成失败:"+e.getMessage());
        }
        return success();
    }

    @GetMapping("/order/repair/list")
    public TableDataInfo repairList(WorkerOrderQuery query)
    {
        startPage();
        Repair repair = new Repair();
        repair.setWorkerId(query.getWorkerId());
        repair.setType(query.getType());
        List<Repair> list = repairService.selectRepairList(repair);
        return getDataTable(list);
    }

    @GetMapping("/order/repair/getFinishCode")
    public AjaxResult getRepairFinishCode(Long id)
    {
        Repair repair = repairService.selectRepairById(id);
        if(repair!=null && repair.getFinishCode()!=null){
            //发送验证码
            Map<String,String> paramsMap = new HashMap<>();
            paramsMap.put("code",repair.getFinishCode());
            paramsMap.put("orderSn",repair.getWorkNo());
            aliyunSmsClient.sendSms("work",repair.getContactNumber(),paramsMap);
            return AjaxResult.success();
        }
        return AjaxResult.error("验证码发送失败！");
    }

    @PostMapping("/order/repair/completed")
    public AjaxResult completedRepair(@RequestBody WorkOrderFinishedReq req) {
        try {
            repairService.completedRepair(req.getId(), req.getFinishCode());
        } catch (Exception e) {
            log.error("工单完成失败,errMsg:{}",e.getMessage(),e);
            return error(e.getMessage());
        }
        return success();
    }
}
